package com.cong.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cong.project.common.BaseResponse;
import com.cong.project.common.ErrorCode;
import com.cong.project.common.ResultUtils;
import com.cong.project.exception.BusinessException;
import com.cong.project.model.entity.MerchantMessage;
import com.cong.project.model.entity.Message;
import com.cong.project.model.request.MerchantAddRequest;
import com.cong.project.model.request.MerchantSearchRequest;
import com.cong.project.model.request.TokenUserRequest;
import com.cong.project.model.vo.MerChantVo;
import com.cong.project.service.MerchantMessageService;
import com.google.gson.Gson;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 商户
 * @author lhc
 * @date 2022-09-22 10:39
 */
@RestController
@RequestMapping("merchant")
public class MerchantController {

    @Resource
    private MerchantMessageService merchantMessageService;
    /**
     * 查询商户列表
     * @param merchantSearchRequest
     * @return
     */
    @GetMapping("/list")
    public BaseResponse getMerchantList( MerchantSearchRequest merchantSearchRequest){
        if (merchantSearchRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        IPage<MerChantVo> page =  merchantMessageService.getShopList(merchantSearchRequest);

        return ResultUtils.success(page);
    }
    /**
     * 查询商户详情
     * @param merchantId
     * @return
     */
    @GetMapping("/detail")
    public BaseResponse getMerchantDetail( Long merchantId){
        if (merchantId==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        MerChantVo merChantVo = merchantMessageService.getMerchantDetail(merchantId);

        return ResultUtils.success(merChantVo);
    }
    /**
     * 获取我创建的店铺信息
     * @param merchantSearchRequest
     * @return
     */
    @GetMapping("/list/my/store")
    public BaseResponse getMerchantMyList( MerchantSearchRequest merchantSearchRequest,TokenUserRequest tokenUserRequest){
        if (merchantSearchRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //把登录用户的id设置进去
        merchantSearchRequest.setUserId(tokenUserRequest.getId());

        IPage<MerChantVo> page =  merchantMessageService.getShopList(merchantSearchRequest);

        return ResultUtils.success(page);
    }

    /**
     * 添加商铺表
     * @param merchantAddRequest
     * @param tokenUserRequest
     * @return
     */
    @PostMapping("/add")
    public BaseResponse addMerchant(@RequestBody MerchantAddRequest  merchantAddRequest,TokenUserRequest tokenUserRequest){
        if (merchantAddRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = merchantMessageService.addMerchant(merchantAddRequest,tokenUserRequest);
        return ResultUtils.success(result);
    }
}
